package com.mxx.common.netty.http;

import com.mxx.common.netty.http.resolvers.NettyResolvers;
import com.mxx.common.netty.http.validate.ParameterValidator;
import lombok.Data;
import lombok.experimental.Accessors;

import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Parameter;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: mxx
 * @date: 2021/9/24
 * @time: 10:42
 * @Description: 描述信息
 */
@Data
@Accessors(chain = true)
public class NettyMethodParameter {

    private NettyMethod method;
    private Parameter parameter;
    private Integer index;
    private NettyResolvers resolvers;
    private List<ParameterValidator> validates;


    public NettyMethodParameter(NettyMethod method, Parameter parameter, Integer index) {
        this.method = method;
        this.parameter = parameter;
        this.index = index;
        this.validates = new ArrayList<>();
    }

    public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
        return this.parameter.getAnnotation(annotationClass);
    }

    public Object getVal(NettyHttpRequest request) throws IOException {
        if (this.resolvers == null) {
            return null;
        }
        Object data = resolvers.resolvers(this.method, request, this);
        return data;
    }


    public String getName() {
        return this.parameter.getName();
    }

    public Class<?> getType() {
        return this.parameter.getType();
    }


}
